package com.redoor.app.stake.basicdata.mq.consumerService;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.redoor.app.stake.basicdata.model.constants.BasePropConstants;
import com.redoor.app.stake.basicdata.service.BasePropService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.Properties;

/**
 * Created by zhangYiHang on 2018/4/10.
 */
public class UpdateOrganizationMQListenerService {
    private static final Logger logger = LoggerFactory.getLogger(UpdateOrganizationMQListenerService.class);

    private Consumer consumer ;

    @Autowired
    private BasePropService basePropService;

    @PostConstruct
    public void  init(){
        try {
            logger.info("init UpdateOrganizationMQListenerService ...");
            Properties cprop = new Properties();
            cprop.put(PropertyKeyConst.AccessKey, this.basePropService.getProp(BasePropConstants.consumerAccessKey));
            cprop.put(PropertyKeyConst.SecretKey, this.basePropService.getProp(BasePropConstants.consumerSecretKey));
            cprop.put(PropertyKeyConst.ONSAddr, this.basePropService.getProp(BasePropConstants.namesrvAddr));
            cprop.put(PropertyKeyConst.ConsumerId, this.basePropService.getProp(BasePropConstants.customerId3));
            if(basePropService.initMQFlag()) {
                consumer = ONSFactory.createConsumer(cprop);
                consumer.subscribe("qfjs_sys_org", "create||update||delete", new UpdateOrganizationMessageListener());
                consumer.start();
                logger.info("init UpdateOrganizationMQListenerService successfully get mq consumer = " + consumer);
            }else {
                logger.warn("not init UpdateOrganizationMQListenerService because flag is flase");
            }
        } catch (Exception e){
            logger.error("init UpdateOrganizationMQListenerService failure",e);
        }

    }
}
